package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import model.Mode;

import controller.Controller;

public class Gui implements ActionListener{
	private JFrame frame;
	private Controller controller;
	private JTextField txtFilename;
	private JRadioButton btnHostname, btnIPs;

	public Gui(Controller controller) {
		setupScreen();
		this.controller = controller;
		
		//set the frame to a Border Layout
		frame.setLayout(new BorderLayout());
		
		//create the resolve button and add it to the south of the frame
		JButton btnResolve = new JButton("Resolve addresses");
		frame.add(btnResolve, BorderLayout.SOUTH);
		
		JPanel pnlGrid = new JPanel();
		pnlGrid.setLayout(new GridLayout(2,1));
		frame.add(pnlGrid, BorderLayout.CENTER);
		
		//create a panel that will contain a label, the textfield, and the browse button; and add it to the grid
		JPanel pnlSelect = new JPanel();
		pnlSelect.setLayout(new BorderLayout());
		pnlGrid.add(pnlSelect);
		
		//create label and add it to the selectPanel
		JLabel lblDirections = new JLabel("Select your filename:");
		pnlSelect.add(lblDirections, BorderLayout.NORTH);
		
		//create textfield and add it to the selectPanel
		txtFilename = new JTextField();
		pnlSelect.add(txtFilename, BorderLayout.CENTER);
		
		//create browse button and add it to the selectPanel
		JButton btnBrowse = new JButton("Browse");
		pnlSelect.add(btnBrowse, BorderLayout.EAST);
		
		//create a mode select JPanel and add it to the grid
		JPanel pnlMode = new JPanel();
		pnlMode.setLayout(new BoxLayout(pnlMode, BoxLayout.Y_AXIS));
		pnlGrid.add(pnlMode);
		
		//create buttons and labels for mode select 
		JLabel lblMode = new JLabel("Output to:");
		pnlMode.add(lblMode); 
		btnHostname = new JRadioButton("Hostnames");
		pnlMode.add(btnHostname);
		btnIPs = new JRadioButton("IP Addresses");
		pnlMode.add(btnIPs);
		
		//create buttongroup, add buttons to the group and auto-select Hostnames as the default
		ButtonGroup btngrpModeGrp = new ButtonGroup();
		btngrpModeGrp.add(btnHostname);
		btngrpModeGrp.add(btnIPs);
		btnHostname.setSelected(true);
		
		//sets actionListeners
		btnBrowse.addActionListener(this);
		btnResolve.addActionListener(this);
		
		frame.setVisible(true);
	}
	public boolean selectFile(){
		return controller.selectFile(frame);
	}

	private void setupScreen(){
		frame = new JFrame("Host Resolver (8-17-11)");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int width = screen.width/3;
		int height = screen.height/3;
		frame.setSize(width, height);
		frame.setLocation( (((screen.width)/2)-(width/2)), (screen.height/2)-height/2);
	}
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("Browse")){
			if(controller.selectFile(frame)== true){
				txtFilename.setText(controller.getSelectedFileOrDirectoryFullPath());
			}
		}
		else if (command.equals("Resolve addresses")){
			Mode mode = Mode.HOSTNAME;
			if(btnIPs.isSelected()){
				mode = Mode.IP;
			}
			controller.analyzeFiles(frame, new String[]{txtFilename.getText()}, mode);
		}
		
	}
	
}
